
/**
 * Created on 2016/9/22
 */

var baseUrl = "http://192.168.2.205:9999/ebjcfsweb/";
// 图片路径
var imgsrc = baseUrl + "path/";
// 获得参数，签名
var tpcName = GetQueryString('tpcId'); 
var authToken = "QYVD8W2JJW4KYDP2YMKWFX36";

$(function ()
{
    // 帖子标题
    var appTitle = document.querySelector("#appTitle");
    // 用户昵称
    var nickName = document.querySelector(".header-text");
    // 提交时间
    var smtTime = document.querySelector(".date-text");
    // 头像路径
    var photoPath = document.querySelector("#photoPath");
    var errorBox = document.querySelector("#error-box");
    var respondBox = document.querySelector("#respondBox");
    var detailsEle = $(".editable-text-content");
    var editerEle = $(".editer-text");
    var addCollect = $(".add-collection");
   
    var tpcId = '{"tpcId":"'+tpcName+'"}';
    var nowDate = new Date();
    var sendTime = nowDate.getTime();
    var signTopic = Crypto.MD5("sendTime="+sendTime+'data={"tpcId":"'+tpcName+'"}').toUpperCase();

    // 启用的帖子
    $.ajax({
        url : baseUrl + 'getTopic',
        type : "POST",
        dataType : 'json',
        async : true,
        data :
        {
            sign: signTopic, 
            signType: '01', 
            data: tpcId, 
            authToken: "QYVD8W2JJW4KYDP2YMKWFX36", 
            sendTime: sendTime, 
            appType: '01', 
            version: '1.0'
        },
        success: function(res){
          var data = res.data;
          var result = JSON.parse(data);
          var resultList = result.list; 
          if (res.rspCode == "0000")
          {
              appTitle.innerText = result.name;
              nickName.innerText = result.nickName;
              smtTime.innerText = result.smtTime;            
              photoPath.setAttribute("src", imgsrc + result.photoPath);
              // 显示帖子详情
              if(result.list.length > 0){
                for(var i=0; i< result.list.length; i++){
                    var content = '<div class="made-detail-title">'+
                                   '<p>'+result.list[i].text+'</p>'+
                                   '<img class="imgArr" src="'+imgsrc+result.list[i].imgPath+'">'+
                                  '</div>';
                    $("#respondMade").append(content);    
                };  
                // 图片路径为空
                var imgArr=[];
                for (var j = 0; j < result.list.length; j++) {
                    if (result.list[j].imgPath == '') {
                       imgArr.push(j); 
                    }
                }
                // 隐藏空路径图片
                for (var i = 0; i < imgArr.length; i++) {
                  $(".imgArr").eq(imgArr[i]).addClass('none');
                }                                 
             }            
          } else {
             $("aside").css("display","none");
             respondBox.style.display = 'none';
             errorBox.style.display = 'block';
          }
          // 回复
          getTpcDetails();
        },
        error: function(){
          alert("访问服务器失败，请稍后重试！");
        }
    });
    

    //返回顶部显示与隐藏
    // window.onscroll = function (){
    //     var scroll = document.body.scrollTop || document.documentElement.scrollTop,
    //         gotop = document.querySelector(".go-top-box");

    //     if (scroll > $(window).height()){
    //         gotop.classList.remove("none");
    //     }else{
    //         gotop.classList.add("none");
    //     }
    // };

    // // 返回顶部
    // $("#goTop").bind("touchstart", function (){
    //     $("html, body").animate({
    //         scrollTop : 0
    //     }, 300);
    // });

    // 咖啡挑选详情文字
    function detailsClass(target,obj){
        $(target).toggleClass(obj);
    };

    detailsEle.on("click", function (e){
        e.preventDefault();      
        e.stopPropagation();
        var target = e.target;
        detailsClass(target,'text-hide-ellipsis');
    });

   // 微信下载
   var $downloadBtns = $('.location-download-coffee');
   var $browserTips = $("#browserTips");

   $downloadBtns.on("touchstart", function (e)
    {
        e.preventDefault();      
        e.stopPropagation();
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger")
        {
            $browserTips.show();
        }
        $browserTips.find(".browser-tips-mask").on("touchstart", function (event)
        {
            event.preventDefault();      
            event.stopPropagation();
            $browserTips.hide();
        });
    });

});


// 帖子回应
function getTpcDetails(){
    var listId = '{"type":"topic","id":"'+tpcName+'"}';
    var nowReplyDate = new Date();
    var sendReplyTime = nowReplyDate.getTime();
    var signReply = Crypto.MD5("sendTime="+sendReplyTime+'data={"type":"topic","id":"'+tpcName+'"}').toUpperCase();
    
    // 帖子回复列表
    $.ajax({
        url : baseUrl + 'getReplyList',
        type : 'POST',
        dataType : 'json',
        async : true,
        data :
        {
            sign : signReply,
            signType : '01',
            data : listId,
            authToken : authToken,
            sendTime : sendReplyTime,
            appType : '01',
            version : '1.0'
        },
        success : function (res) {
           var results = JSON.parse(res.data);
           var resultsList = results.list;
           if (res.rspCode == "0000"){  

              if(results.count == 0){
                  document.querySelector("#error-text").style.display = 'block';
              } 

              for (var i = 0; i < resultsList.length; i++) {
                 var contents = '<div class="respond-details-header">'+
                       '<ul><li><img src="'+imgsrc+resultsList[i].photoPath+'"></li>'+
                       '<li><p class="header-text">'+resultsList[i].nickName+'</p><p class="date-text">'+resultsList[i].smtTime+'</p></li></ul>'+
                       '<div class="respond-box-text">'+
                          '<p class="editable-text-content text-hide-ellipsis">'+resultsList[i].cont+'</p>'+
                          '<div class="made-respond-edite"><p class="identity-href"><a href="" id="identityHref">回复</a></p></div>'+
                       '</div>'+
                    '</div>';                 
                 $("#respondBox").append(contents);
              }
           }
        },
        error : function(){
           alert("服务器繁忙，请稍后再试");
        }
    });
}



// 获得url参数
function GetQueryString(name) {
   var url = location.search; 
   var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
   var r = window.location.search.substr(1).match(reg);
   if (r != null) return decodeURI(r[2]); return null;
}